﻿namespace Shop.Core.Share
{
    /// <summary>
    /// 分页对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PageModel<T>
    {
        private IReadOnlyList<T> _data = Array.Empty<T>();

        public PageModel()
        {
        }

        public PageModel(IPageSearchModel search)
            : this(search, default, default)
        {
        }

        public PageModel(IPageSearchModel search, List<T> data, int count)
            : this(search.PageIndex, search.PageSize, data, count)
        {
        }

        public PageModel(int pageIndex, int pageSize, List<T> data, int count)
        {
            this.PageIndex = pageIndex;
            this.PageSize = pageSize;
            this.TotalCount = count;
            this.Data = data;
        }

        public IReadOnlyList<T> Data
        {
            get => _data;
            set
            {
                if (value != null)
                {
                    _data = value;
                }
            }
        }

        public int RowsCount { get { return _data.Count; } }

        public int PageIndex { get; set; }

        public int PageSize { get; set; }

        public int TotalCount { get; set; }

        public int PageCount { get { return ((this.RowsCount + this.PageSize - 1) / this.PageSize); } }
    }
}
